home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / SCRIB4.PAK / SCRIBDOC.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  7KB  |  273 lines

  1. // ScribDoc.cpp : implementation of the CScribbleDoc class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1995 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "Scribble.h"
  15.  
  16. #include "ScribDoc.h"
  17. #include "PenDlg.h"
  18.  
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CScribbleDoc
  27.  
  28. IMPLEMENT_DYNCREATE(CScribbleDoc, CDocument)
  29.  
  30. BEGIN_MESSAGE_MAP(CScribbleDoc, CDocument)
  31.     //{{AFX_MSG_MAP(CScribbleDoc)
  32.     ON_COMMAND(ID_EDIT_CLEAR_ALL, OnEditClearAll)
  33.     ON_COMMAND(ID_PEN_THICK_OR_THIN, OnPenThickOrThin)
  34.     ON_UPDATE_COMMAND_UI(ID_EDIT_CLEAR_ALL, OnUpdateEditClearAll)
  35.     ON_UPDATE_COMMAND_UI(ID_PEN_THICK_OR_THIN, OnUpdatePenThickOrThin)
  36.     ON_COMMAND(ID_PEN_WIDTHS, OnPenWidths)
  37.     //}}AFX_MSG_MAP
  38. END_MESSAGE_MAP()
  39.  
  40. /////////////////////////////////////////////////////////////////////////////
  41. // CScribbleDoc construction/destruction
  42.  
  43. CScribbleDoc::CScribbleDoc()
  44. {
  45.     // TODO: add one-time construction code here
  46.  
  47. }
  48.  
  49. CScribbleDoc::~CScribbleDoc()
  50. {
  51. }
  52.  
  53. BOOL CScribbleDoc::OnNewDocument()
  54. {
  55.     if (!CDocument::OnNewDocument())
  56.         return FALSE;
  57.     InitDocument();
  58.     return TRUE;
  59. }
  60.  
  61. /////////////////////////////////////////////////////////////////////////////
  62. // CScribbleDoc serialization
  63.  
  64. void CScribbleDoc::Serialize(CArchive& ar)
  65. {
  66.     if (ar.IsStoring())
  67.     {
  68.         ar << m_sizeDoc;
  69.     }
  70.     else
  71.     {
  72.         ar >> m_sizeDoc;
  73.     }
  74.     m_strokeList.Serialize(ar);
  75. }
  76.  
  77. /////////////////////////////////////////////////////////////////////////////
  78. // CScribbleDoc diagnostics
  79.  
  80. #ifdef _DEBUG
  81. void CScribbleDoc::AssertValid() const
  82. {
  83.     CDocument::AssertValid();
  84. }
  85.  
  86. void CScribbleDoc::Dump(CDumpContext& dc) const
  87. {
  88.     CDocument::Dump(dc);
  89. }
  90. #endif //_DEBUG
  91.  
  92. /////////////////////////////////////////////////////////////////////////////
  93. // CScribbleDoc commands
  94.  
  95. BOOL CScribbleDoc::OnOpenDocument(LPCTSTR lpszPathName) 
  96. {
  97.     if (!CDocument::OnOpenDocument(lpszPathName))
  98.         return FALSE;
  99.     InitDocument(); 
  100.     return TRUE;
  101. }
  102.  
  103. void CScribbleDoc::DeleteContents() 
  104. {
  105.     while (!m_strokeList.IsEmpty())
  106.     {
  107.         delete m_strokeList.RemoveHead();
  108.     }
  109.     CDocument::DeleteContents();
  110. }
  111.  
  112. void CScribbleDoc::InitDocument()
  113. {
  114.     m_bThickPen = FALSE;
  115.     m_nThinWidth = 2;   // default thin pen is 2 pixels wide
  116.     m_nThickWidth = 5;  // default thick pen is 5 pixels wide
  117.     ReplacePen();       // initialize pen according to current width
  118.  
  119.     // default document size is 800 x 900 screen pixels
  120.     m_sizeDoc = CSize(800,900);
  121. }
  122.  
  123. CStroke* CScribbleDoc::NewStroke()
  124. {
  125.     CStroke* pStrokeItem = new CStroke(m_nPenWidth);
  126.     m_strokeList.AddTail(pStrokeItem);
  127.     SetModifiedFlag();  // Mark the document as having been modified, for
  128.                         // purposes of confirming File Close.
  129.     return pStrokeItem;
  130. }
  131.  
  132.  
  133.  
  134.  
  135. /////////////////////////////////////////////////////////////////////////////
  136. // CStroke
  137.  
  138. IMPLEMENT_SERIAL(CStroke, CObject, 2)
  139. CStroke::CStroke()
  140. {
  141.     // This empty constructor should be used by serialization only
  142. }
  143.  
  144. CStroke::CStroke(UINT nPenWidth)
  145. {
  146.     m_nPenWidth = nPenWidth;
  147.     m_rectBounding.SetRectEmpty();
  148. }
  149.  
  150. void CStroke::Serialize(CArchive& ar)
  151. {
  152.     if (ar.IsStoring())
  153.     {
  154.         ar << m_rectBounding;
  155.         ar << (WORD)m_nPenWidth;
  156.         m_pointArray.Serialize(ar);
  157.     }
  158.     else
  159.     {
  160.         ar >> m_rectBounding;
  161.         WORD w;
  162.         ar >> w;
  163.         m_nPenWidth = w;
  164.         m_pointArray.Serialize(ar);
  165.     }
  166. }
  167.  
  168. BOOL CStroke::DrawStroke(CDC* pDC)
  169. {
  170.     CPen penStroke;
  171.     if (!penStroke.CreatePen(PS_SOLID, m_nPenWidth, RGB(0,0,0)))
  172.         return FALSE;
  173.     CPen* pOldPen = pDC->SelectObject(&penStroke);
  174.     pDC->MoveTo(m_pointArray[0]);
  175.     for (int i=1; i < m_pointArray.GetSize(); i++)
  176.     {
  177.         pDC->LineTo(m_pointArray[i]);
  178.     }
  179.  
  180.     pDC->SelectObject(pOldPen);
  181.     return TRUE;
  182. }
  183. void CScribbleDoc::OnEditClearAll() 
  184. {
  185.     DeleteContents();
  186.     SetModifiedFlag();  // Mark the document as having been modified, for
  187.                         // purposes of confirming File Close.
  188.     UpdateAllViews(NULL);
  189. }
  190.  
  191. void CScribbleDoc::OnPenThickOrThin() 
  192. {
  193.     // Toggle the state of the pen between thin or thick.
  194.     m_bThickPen = !m_bThickPen;
  195.  
  196.     // Change the current pen to reflect the new user-specified width.
  197.     ReplacePen();
  198. }
  199.  
  200. void CScribbleDoc::ReplacePen()
  201. {
  202.     m_nPenWidth = m_bThickPen? m_nThickWidth : m_nThinWidth;
  203.  
  204.     // Change the current pen to reflect the new user-specified width.
  205.     m_penCur.DeleteObject();
  206.     m_penCur.CreatePen(PS_SOLID, m_nPenWidth, RGB(0,0,0)); // solid black
  207. }
  208.  
  209. void CScribbleDoc::OnUpdateEditClearAll(CCmdUI* pCmdUI) 
  210. {
  211.     // Enable the command user interface object (menu item or tool bar
  212.     // button) if the document is non-empty, i.e., has at least one stroke.
  213.     pCmdUI->Enable(!m_strokeList.IsEmpty());
  214. }
  215.  
  216. void CScribbleDoc::OnUpdatePenThickOrThin(CCmdUI* pCmdUI) 
  217. {
  218.     // Add check mark to Draw Thick Line menu item, if the current
  219.     // pen width is "thick".
  220.     pCmdUI->SetCheck(m_bThickPen);
  221. }
  222.  
  223. void CScribbleDoc::OnPenWidths() 
  224. {
  225.     CPenWidthsDlg dlg;
  226.     // Initialize dialog data
  227.     dlg.m_nThinWidth = m_nThinWidth;
  228.     dlg.m_nThickWidth = m_nThickWidth;
  229.  
  230.     // Invoke the dialog box
  231.     if (dlg.DoModal() == IDOK)
  232.     {
  233.         // retrieve the dialog data
  234.         m_nThinWidth = dlg.m_nThinWidth;
  235.         m_nThickWidth = dlg.m_nThickWidth;
  236.  
  237.         // Update the pen that is used by views when drawing new strokes,
  238.         // to reflect the new pen width definitions for "thick" and "thin".
  239.         ReplacePen();
  240.     }
  241. }
  242.  
  243. void CStroke::FinishStroke()
  244. {
  245.     // Calculate the bounding rectangle.  It's needed for smart
  246.     // repainting.
  247.  
  248.     if (m_pointArray.GetSize()==0)
  249.     {
  250.         m_rectBounding.SetRectEmpty();
  251.         return;
  252.     }
  253.     CPoint pt = m_pointArray[0];
  254.     m_rectBounding = CRect(pt.x, pt.y, pt.x, pt.y);
  255.  
  256.     for (int i=1; i < m_pointArray.GetSize(); i++)
  257.     {
  258.         // If the point lies outside of the accumulated bounding
  259.         // rectangle, then inflate the bounding rect to include it.
  260.         pt = m_pointArray[i];
  261.         m_rectBounding.left     = min(m_rectBounding.left, pt.x);
  262.         m_rectBounding.right    = max(m_rectBounding.right, pt.x);
  263.         m_rectBounding.top      = min(m_rectBounding.top, pt.y);
  264.         m_rectBounding.bottom   = max(m_rectBounding.bottom, pt.y);
  265.     }
  266.  
  267.     // Add the pen width to the bounding rectangle.  This is necessary
  268.     // to account for the width of the stroke when invalidating
  269.     // the screen.
  270.     m_rectBounding.InflateRect(CSize(m_nPenWidth, m_nPenWidth));
  271.     return;
  272. }
  273.